package ui.custom_context_menu

/**
 * @author wei kunJie
 * @date 2024/3/13 16:10:29
 */
import androidx.compose.foundation.*
import androidx.compose.foundation.ContextMenuArea
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.text.LocalTextContextMenu
import androidx.compose.foundation.text.TextContextMenu
import androidx.compose.foundation.text.TextContextMenu.TextManager
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.platform.ClipboardManager
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalLocalization
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.text.AnnotatedString
import java.net.URLEncoder
import java.nio.charset.Charset

//剪切板
var clipboardManager: ClipboardManager? = null

@ExperimentalFoundationApi
val CustomLocalTextContextMenu:
        ProvidableCompositionLocal<CustomTextContextMenu> = staticCompositionLocalOf { CustomTextContextMenu }

@OptIn(ExperimentalFoundationApi::class)
object CustomTextContextMenu : TextContextMenu {
    var copyText: String = ""

    @Composable
    override fun Area(textManager: TextManager, state: ContextMenuState, content: @Composable () -> Unit) {
        if (clipboardManager == null) {
            clipboardManager = LocalClipboardManager.current
        }
        val items = {
            if (textManager.selectedText.isEmpty()) {
                listOfNotNull(
                    ContextMenuItem("复制") { clipboardManager?.setText(AnnotatedString(copyText)) },
                    ContextMenuItem("转发") {/*do something here*/ },
                )
            } else {
                listOfNotNull(
                    textManager.cut?.let {
                        ContextMenuItem("剪切", it)
                    },
                    textManager.copy?.let {
                        ContextMenuItem("复制", it)
                    },
                    textManager.paste?.let {
                        ContextMenuItem("粘贴", it)
                    },
                    textManager.selectAll?.let {
                        ContextMenuItem("全选", it)
                    },
                )
            }
        }

        ContextMenuArea(items, state, content = content)
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CustomTextMenuProvider(content: @Composable () -> Unit) {
    val textMenu = CustomLocalTextContextMenu.current
    val uriHandler = LocalUriHandler.current
    CompositionLocalProvider(
        LocalTextContextMenu provides object : TextContextMenu {
            @Composable
            override fun Area(
                textManager: TextManager,
                state: ContextMenuState,
                content: @Composable () -> Unit
            ) {
                // Here we reuse the original TextContextMenu, but add an additional item to item on the bottom.
                ContextMenuDataProvider({
                    val shortText = textManager.selectedText.crop()
                    if (shortText.isNotEmpty()) {
                        val encoded = URLEncoder.encode(shortText, Charset.defaultCharset())
                        listOf(
                            ContextMenuItem("转发") {},
                            ContextMenuItem("百度一下") { uriHandler.openUri("https://www.baidu.com/s?wd=$encoded") }
                        )
                    } else {
                        emptyList()
                    }
                }) {
                    textMenu.Area(textManager, state, content = content)
                }
            }
        },
        content = content
    )
}

private fun AnnotatedString.crop() = toString()
